<?php

/**
 * Slideshow Helper for Cake.
 * 
 *
 * PHP versions 4 and 5
 * 
 *  heavily based on the Pagination work of PJ Holden (pjholden@gmail.com)
 *
 * @filesource 
 * @author       Christophe Vandeplas
 * @link         https://trac.cakephp.org/wiki/Authors Authors/Developers
 * @version      $Revision: 0.1 $
 * @modifiedby   $LastChangedBy: pj $
 * @license      GNU General Public License v2
 */

/**
 * Enter description here...
 */
uses('object');

class Slideshow extends Object
{
    var $_pageDetails = array();
 	/**
        * Internal error display should the pageDetails array not be intialised, and DEBUG be set to 1 or 2 this will display an error
        *
        * @param string $text - error text to use
        **/
    function _err( $text=' Page Details have not been passed' )
    {
        if (DEBUG)
        {
            echo "<br/>Slideshow Error: $text<br/>";
            if (DEBUG>1) { debug($this->_paginationDetails); };
        }
    }
   /**
    * Returns a two element array
    * element 1 - array of pageDetails
    * element 2 - the results of the findAll on the model passed
    * 
    *  pageDetails is an array of the form:
    *  page => current page
    *  recordCount => number of records matching criteria
    *  pageCount => total number of pages
    *  nextPageNumber => next page number (or empty string)
    *  previousPageNumber => previous page numebr (or empty string)
    *  limit => limit used (default 50)
    * 
    * The parameters are passed as a hash array, so, for example $pagination->findAll( array('model'=>$this->models['user'], 'criteria'=>'surname like "%A%"'))
    * @params object $model - the model to be used for the query
    * @params int $page - the current page
    * @params int $limit - records per page
    * @params string $criteria - the criteria to use
    * @params array $fields - list of fields to return
    * @params string $order - the order to place the records
    **/
		 //$model, $page=1,  $criteria = null, $order = null, $fields = null, $limit=50
    function findAll(&$model, $parameters=array())
    {
	 	// set sensible defaults ...
	    $page = 1;
	    $criteria = null;
	    $order = null;
	    $fields = null;
	    $limit = 50;
			// extract the arguments array into the local variables ...				
	    extract($parameters);
	    $feed = ($feed != 0) ? $feed : 1;
	    $recordCount = $model->findCount($criteria); // number of possible results
	    $this->_pageDetails = array(
	                'page'=>$page,
	                'recordCount'=>$recordCount,
	                'nextPageNumber'=> ($page < $recordCount) ? $page+1 : 1,
	                'previousPageNumber'=> ($page>1) ? $page-1 : $recordCount,
	                'limit'=>$limit
	    );
	    $data = $model->findAll($criteria, $fields, $order, $limit, $page);
	    return $data;
    }
    
    
   /**
    * Displays a link to the previous page, where the page doesn't exist then
    * display the last page
    * 
    * @param string $link - Actual link for page (use %s in place of the page number)
    **/
    function previousPage( $link='&page=%s' )
    {
        if (empty($this->_pageDetails)) { return $this->_err(); }
        $link = empty($link) ? $this->_defaultLink : $link;
        $t ='';
        if ( !empty($this->_pageDetails['previousPageNumber']) ) {
            $t = sprintf($link, $this->_pageDetails['previousPageNumber'] );
        }
        return $t;
    }
   /**
    * Displays the link to the next page, where the page doesn't exist then
    * display the first page
    *
    * @param string $link - Actual link for page (use %s in place of the page number)
    **/
    function nextPage( $link='&page=%s' )
    {
        if (empty($this->_pageDetails)) { return $this->_err(); }
        $link = empty($link) ? $this->_defaultLink : $link;
        $t = '';
        if (!empty($this->_pageDetails['nextPageNumber'])) {
            $t = sprintf($link, $this->_pageDetails['nextPageNumber']);
        }
        return $t;
    }
    
   /**
    * Displays the link to the first page
    * 
    * @param string $link - Actual link for page (use %s in place of the page number)
    **/
    function firstPage( $link='page=%s' )
    {
        if(empty($this->_pageDetails)) {return $this->_err(); }
        $link = empty($link) ? $this->_defaultLink : $link;
        return sprintf($link, 1);
    }
    
   /**
    * Displays the link to the last page
    * 
    * @param string $link - Actual link for page (use %s in place of the page number)
    **/
    function lastPage ( $link='page=%s' )
    {
        if(empty($this->_pageDetails)) {return $this->_err(); }
        $link = empty($link) ? $this->_defaultLink : $link;
        return sprintf($link, $this->_pageDetails['recordCount']);
    }
}
?>